package com.baixiaowen.xiaoaointerview.并发提升篇.N种优化哲学家就餐问题的方法;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Baixiaowen
 */
public class DiningPhilosophersDeadLock{

    // 哲学家
    Phi[] phis = new Phi[5];
    // 叉子
    volatile int[] forks = new int[5];

    ReentrantLock lock = new ReentrantLock();

    Condition[] waitForks = new Condition[5];

    Condition wait = lock.newCondition();

    public DiningPhilosophersDeadLock() {
        for (int i = 0; i < 5; i++) {
            phis[i] = new Phi(i + 1);
            forks[i] = 0;
            waitForks[i] = lock.newCondition();
        }
    }

    class Phi extends Philosopher {

        public Phi(int id) {
            super(id);
        }

        @Override
        protected synchronized boolean takeLeft(int[] forks) {
            return super.takeLeft(forks);
        }

        @Override
        protected synchronized boolean takeRight(int[] forks) {
            return super.takeRight(forks);
        }

        @Override
        public void run() {
            while (true) {
                try {
//                    this.thinking();
//                    while (!this.takeLeft(forks)) {
//                        Thread.sleep(0);
//                    }
////                    System.err.println("take left ");
//                    Thread.sleep(100);
////                    System.err.println("take right ");
//                    int c = 0;
//                    // deadlock
//                    // livelock
//                    while (!this.takeRight(forks)) {
//                        c ++;
//                        if (c > 100) {
//                            this.putLeft(forks);
//                            continue;
//                        }
//                        Thread.sleep(0);
//                    }
//                    this.eating();
//                    this.putLeft(forks);
//                    this.putRight(forks);
//                    this.finished();


                    this.thinking();
                    lock.lockInterruptibly();

                    while (!this.takeLeft(forks)) {
                        wait.await();
//                        waitForks[this.left()].await();
                    }

                    while (!this.takeRight(forks)) {
                        wait.await();
//                        waitForks[this.right()].await();
                    }

//                    if (!this.checkLeft(forks) && this.checkRight(forks)) {
//                        lock.unlock();
//                        continue;
//                    }
//                    this.takeLeft(forks);
//                    this.takeRight(forks);

//                    while (!this.takeLeft(forks)) {
//                        Thread.sleep(0);
//                    }
//                    while (!this.takeRight(forks)) {
//                        Thread.sleep(0);
//                    }
                    lock.unlock();
                    this.eating();


                    lock.lockInterruptibly();
                    this.putLeft(forks);
                    wait.signalAll();
//                    waitForks[this.left()].signalAll();
//                    waitForks[this.right()].signalAll();
                    this.putRight(forks);
                    lock.unlock();
                    this.finished();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }
    }

    public void run() {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            pool.submit(phis[i]);
        }
    }

    public static void main(String[] args) {
        DiningPhilosophersDeadLock solver = new DiningPhilosophersDeadLock();
        solver.run();
    }

}
